package com.myebills.ebpsfrontend.cust.controller;


/**
 * Module Name  : EbppxValidation
 * Description  : some common validation methods
 * Last Updated : 2002-09-25
 *
 * @author lhlue
 *
 * Remarks :
 *
 */
public class EbppxValidation {

	//character set can be used as parameter for isValidString()
	public static String ALPHANUMERIC="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	public static String CHARACTER="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_";
    public static String LOW_CASE_CHARACTER="abcdefghijklmnopqrstuvwxyz";
    public static String UP_CASE_CHARACTER="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static String INTEGER="0123456789";
	public static String FLOAT="0123456789.";

    /**
     * Validate the userId String
     *
     * @param   inStr - the given String to validate
	 * @param   charSet - All the characters allowed
     * @param   nullAble - the String can be null or empty
     * @param   minLength - minimum length of the string, negative integer to disable the option
     * @param   maxLength - maximum length of the string, negative integer to disable the option
	 *
     * @return  the flag indicating the validity of the given String
     */
     public static boolean isValidString(String inStr, String charSet, boolean nullAble, int minLength, int maxLength)
     {
		 //Check for nullable
		 if ( nullAble && EbppxUtil.nullToEmpty(inStr).equals(""))
		 {
			 return true;
		 }
		 else if ( !nullAble && EbppxUtil.nullToEmpty(inStr).equals(""))
		 {
			 return false;
		 }
		 //Check for minimum length
		 else if ( minLength >0 && inStr.length() < minLength )
		 {
			 return false;
		 }
		 //Check for maximum length
		 else if ( maxLength >0 && inStr.length() > maxLength )
		 {
			 return false;
		 }
		 //Check for valid character sets
		 else
		 {
			 boolean flag=true;

             for (int i=0; i<inStr.length(); i++ )
             {
				 if (charSet.indexOf(inStr.charAt(i)) == -1)
				 {
					 flag=false;
					 break;
				 }
             }

			 if (flag == true)
				 return true;
			 else
				 return false;
		 }
     }


    /**
     * Validate the email (x@x.x)
     *
     * @param strEmail - String of an email
     * @param nullAble - the email can be null or empty
     *
     * @return the flag indicating the validity of the given email
     */
	public static boolean isValidEmail(String strEmail, boolean nullAble)
	{
		 int  length = strEmail.length();
		 int  aliasIndex=-1;
		 char unit;

		 boolean valid=true;


		 //Check for nullable
		 if ( nullAble && EbppxUtil.nullToEmpty(strEmail).equals(""))
		 {
			 return true;
		 }
		 else if ( !nullAble && EbppxUtil.nullToEmpty(strEmail).equals(""))
		 {
			 return false;
		 }

		// Check :
		//   i) Character allowed : alphanumeric, underscore, dash,
		//							period and alias.
		//  ii) Email address starts with alphabet
		// iii) Email address contains alias (@)
		//  iv) Alias (@) exists only once
		//   v) Period (.) exist in substring after alias (@)
		//  vi) Character '@' must follow by an alphabet
		// vii) No consecutive period
		//  ix) Email address ends with alphabet
/*
		for (int i=0; i<length; i++)
		{
			unit = strEmail.charAt(i);

			if (!(Character.isLetterOrDigit(unit) || unit == '_' ||
				 unit == '-' || unit == '.' || unit == '@'))
            {
				valid=false;
				break;
			}

			if (unit == '.' && i<length-1 && strEmail.charAt(i+1) == '.')
            {
				valid=false;
				break;
			}
		}

		if (valid && !Character.isLetter(strEmail.charAt(0))) // Start with alphabet
			valid=false;

*/
		if (valid && ((aliasIndex = strEmail.indexOf((int)'@')) == -1 || (aliasIndex == length - 1))) // Character '@' exists and not the last character
			valid=false;


		String backStr = strEmail.substring(aliasIndex + 1);

		if (valid && (backStr.indexOf((int)'@') != -1)) // Character '@' exists once
			valid=false;

		if (valid && (backStr.indexOf((int)'.') == -1)) // period exist after the character '@'
			valid=false;

/*
		if (valid && (!Character.isLetter(backStr.charAt(0)))) // Alphabet must follow '@'
			valid=false;

		if (valid && (!Character.isLetter(backStr.charAt(backStr.length() - 1))) ) // End with alphabet
			valid=false;
*/
		return valid;
	}

    /**
     * main - FOR TESTING PURPOSE ONLY
	 *      - use for module testing
     *
	 */
     public static void main (String args[])
	 {
		 System.out.println(EbppxValidation.isValidString("tesing123 4XYz", EbppxValidation.ALPHANUMERIC+" ", false, -1, -1));
 		 System.out.println(EbppxValidation.isValidString(null, EbppxValidation.ALPHANUMERIC+" ", true, -1, -1));
         System.out.println(EbppxValidation.isValidEmail("aa_bb@dd.zz", false));
         System.out.println(EbppxValidation.isValidEmail("aa..aa@dd.zz", false));
         System.out.println(EbppxValidation.isValidEmail("aa@.zz", false));
         System.out.println(EbppxValidation.isValidEmail("aa@dd+.zz", false));
         System.out.println(EbppxValidation.isValidEmail("aa@zz..a", false));
	 }

}